package cxt.cn.community.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cxt.cn.community.dao.buildMapper;
import cxt.cn.community.entity.Build;
import cxt.cn.community.entity.Dto.RBDto;
import cxt.cn.community.entity.Dto.ResultDto;
import cxt.cn.community.entity.Room;
import cxt.cn.community.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/*
 * @Author chenxiaotian
 * @Description:
 * @Date:2023012023/1/1
 * @Time:15:41
 */
@Service
public class BuildingServiceImpl implements BuildingService {


    @Autowired
    buildMapper buildMapper;

    @Override
    public List<Build> getAllBuilding() {
        return buildMapper.selectList(new QueryWrapper<Build>());
    }

    @Override
    public List<ResultDto> getAll() {
        List<Build> builds = buildMapper.selectList(new QueryWrapper<Build>());
        List<ResultDto> ResultDtos = new ArrayList<>();
        for(Build build:builds) {
            List<RBDto> rbDtos = new ArrayList<>();
            ResultDto resultDto = new ResultDto();
            resultDto.setValue(build.getBuildId());
            resultDto.setLabel(build.getBuildName());
            List<Room> rooms = JSONUtil.toList(new JSONArray(build.getRoomId()), Room.class);
            for(Room room:rooms) {
                RBDto rbDto = new RBDto();
                rbDto.setValue((int) room.getRoomId());
                rbDto.setLabel(room.getRoomName());
                rbDtos.add(rbDto);
            }

            resultDto.setChildren(rbDtos);
            ResultDtos.add(resultDto);
        }

        return ResultDtos;
    }


}
